﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class BackSessionProcessBll 
    {
        public static BackSessionCls[] ReadingBackSession(string SiteId, string UserId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableBackSession where frkUserId=@UserId order by EntryDate DESC",
                    new DbParam[]{
                        new DbParam("UserId",UserId)
                    });
            if (dsResult.Tables[0].Rows.Count ==  0)
            {
                return new BackSessionCls[0];
            }
            BackSessionCls[] CmsBackSessions = BackSessionParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsBackSessions;
        }

        public static BackSessionCls CreateBackSessionModel(string SiteId, string BackSessionId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableBackSession " +
                                               "     where BackSessionId=@BackSessionId",
                    new DbParam[] 
                    { 
                        new DbParam("BackSessionId", BackSessionId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            BackSessionCls OBackSession = BackSessionParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OBackSession;
        }



        public static void AddBackSession(
           string SiteId,
           BackSessionCls OBackSession)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string DelQuery = " Delete from TableBackSession where SessionDate+2<GetDate()";
            DBService.ExecuteNonQuery(null,DelQuery,null);
            DBService.Insert(null, "TableBackSession",
                    new DbParam[] 
                    { 
                        new DbParam("BackSessionId", OBackSession.BackSessionId),
                        new DbParam("Url",OBackSession.Url),
                        new DbParam("SessionDate",System.DateTime.Now),
                        new DbParam("SessionUserId", WebSessionUtility.GetCurrentLoginUser().UserId),
                    });
        }


        public static void DeleteBackSession(string SiteId, string BackSessionId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableBackSession where BackSessionId=@BackSessionId ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("BackSessionId", BackSessionId),
                    });
        }
    }
}

